package com.situ.edu.controller;

import com.situ.edu.pojo.entity.User;
import com.situ.edu.pojo.entity.Xi;
import com.situ.edu.service.IXiService;
import com.situ.edu.service.impl.XiServiceImpl;
import com.situ.edu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/xi")
public class XiController extends BaseController{
    private IXiService xiService = new XiServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "selectAll":
                selectAll(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
        }
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiController.selectById");
        String id = req.getParameter("id");
        User user = xiService.selectById(Integer.parseInt(id));
        toJSON(resp,Result.ok(user));
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiController.selectAll");
        List<Xi> list = xiService.selectAll();
        toJSON(resp, Result.ok(list));
    }
}
